package com.weibo4j.api.util;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.http.Consts;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.message.BasicNameValuePair;

import com.weibo4j.api.WeiboRequest;
import com.weibo4j.api.annotation.NoParam;
import com.weibo4j.api.annotation.Param;

public class UrlGenerateUtil {
    
    public static HttpUriRequest generateUrl(WeiboRequest<?> req) {
        String requestUrl = req.baseUrl();
        HttpUriRequest httpUriRequest = null;
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(req.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            if (req.requestType() == RequestType.GET) {
                requestUrl += "?";
                for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
                    if ("class".equals(propertyDescriptor.getName())) {
                        continue;
                    }
                    Method method = propertyDescriptor.getReadMethod();
                    if (method != null) {
                        NoParam noParam = method.getAnnotation(NoParam.class);
                        if (noParam != null ) {
                            continue;
                        }
                        Object val = method.invoke(req);
                        if (val != null) {
                            if (StringUtils.isNotBlank(val.toString())) {
                                String pm = propertyDescriptor.getName();
                                Param param = method.getAnnotation(Param.class);
                                if (param != null && param.value().length() != 0) {
                                    pm = param.value();
                                }
                                requestUrl += pm + "=" + val.toString() + "&";
                            }
                        }
                    }
                }
                httpUriRequest = new HttpGet(requestUrl);
            } else if (req.requestType() == RequestType.POST) {
                HttpPost post = new HttpPost(requestUrl);
                List<NameValuePair> nnvps = new ArrayList<NameValuePair>();
                for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
                    Method method = propertyDescriptor.getReadMethod();
                    if (method != null) {
                        NoParam noParam = method.getAnnotation(NoParam.class);
                        if (noParam != null) {
                            continue;
                        }
                        Object val = method.invoke(req);
                        if (val != null) {
                            if (StringUtils.isNotBlank(val.toString())) {
                                String pm = propertyDescriptor.getName();
                                Param param = method.getAnnotation(Param.class);
                                if (param != null && param.value().length() != 0) {
                                    pm = param.value();
                                }
                                nnvps.add(new BasicNameValuePair(pm, val.toString()));
                            }
                        }
                    }
                }
                post.setEntity(new UrlEncodedFormEntity(nnvps, Consts.UTF_8));
                httpUriRequest = post;
            }
        } catch (Exception e) {
        }
        return httpUriRequest;
    }
    
    
}
